package cn.edu.zafu.bigdata.lhff;

import java.io.*;
import java.util.*;

public class StudentManagementSystem {
    private static final String FILE_NAME = "students.dat"; // 文件名
    private static List<Student> studentList = new ArrayList<>(); // 存储学生信息的列表

    public static void main(String[] args) {
        loadStudents(); // 加载已有的学生信息
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.println("======= 学生信息管理系统 =======");
            System.out.println("1. 添加学生信息");
            System.out.println("2. 检索学生信息");
            System.out.println("3. 更新学生信息");
            System.out.println("4. 删除学生信息");
            System.out.println("5. 退出程序");
            System.out.print("请输入操作编号 (1-5): ");
            int choice = scanner.nextInt();
            scanner.nextLine();  // 消耗掉换行符

            switch (choice) {
                case 1:
                    addStudent(scanner);
                    break;
                case 2:
                    searchStudent(scanner);
                    break;
                case 3:
                    updateStudent(scanner);
                    break;
                case 4:
                    deleteStudent(scanner);
                    break;
                case 5:
                    saveStudents(); // 保存学生信息到文件
                    System.out.println("退出程序...");
                    return;
                default:
                    System.out.println("无效的选项，请重新输入！");
            }
        }
    }

    // 加载学生信息
    private static void loadStudents() {
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILE_NAME))) {
            Object obj = ois.readObject();
            if (obj instanceof List<?>){
                studentList = (List<Student>) obj;
            }else{
                studentList = new ArrayList<>();
            }
        } catch (IOException | ClassNotFoundException e) {
            System.out.println("未能加载学生信息，可能是第一次运行。");
        }
    }

    // 保存学生信息
    private static void saveStudents() {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(FILE_NAME))) {
            oos.writeObject(studentList);
        } catch (IOException e) {
            System.out.println("保存学生信息失败！");
        }
    }

    // 1. 添加学生信息
    private static void addStudent(Scanner scanner) {
        System.out.print("请输入学号: ");
        String id = scanner.nextLine();
        if (findStudentById(id) != null) {
            System.out.println("学号已存在，请重新输入！");
            return;
        }
        System.out.print("请输入姓名: ");
        String name = scanner.nextLine();
        System.out.print("请输入电话: ");
        String phone = scanner.nextLine();

        Student student = new Student(id, name, phone);
        studentList.add(student);
        saveStudents(); // 添加后保存
        System.out.println("学生信息已添加！");
    }

    // 2. 检索学生信息
    private static void searchStudent(Scanner scanner) {
        while (true) {
            System.out.print("请输入要检索的学号 (输入 'QUIT' 退出): ");
            String id = scanner.nextLine();
            if (id.equalsIgnoreCase("QUIT")) {
                break;
            }
            Student student = findStudentById(id);
            if (student != null) {
                System.out.println(student);
            } else {
                System.out.println("未找到该学生！");
            }
        }
    }

    // 3. 更新学生信息
    private static void updateStudent(Scanner scanner) {
        while (true) {
            System.out.print("请输入要更新的学号 (输入 'QUIT' 退出): ");
            String id = scanner.nextLine();
            if (id.equalsIgnoreCase("QUIT")) {
                break;
            }
            Student student = findStudentById(id);
            if (student == null) {
                System.out.println("未找到该学生！");
                continue;
            }

            System.out.print("请输入新的姓名: ");
            String name = scanner.nextLine();
            System.out.print("请输入新的电话: ");
            String phone = scanner.nextLine();

            student.setName(name);
            student.setPhone(phone);
            saveStudents(); // 更新后保存
            System.out.println("学生信息已更新！");
        }
    }

    // 4. 删除学生信息
    private static void deleteStudent(Scanner scanner) {
        while (true) {
            System.out.print("请输入要删除的学号 (输入 'QUIT' 退出): ");
            String id = scanner.nextLine();
            if (id.equalsIgnoreCase("QUIT")) {
                break;
            }
            Student student = findStudentById(id);
            if (student == null) {
                System.out.println("未找到该学生！");
                continue;
            }

            studentList.remove(student);
            saveStudents();
            System.out.println("学生信息已删除!");
        }
    }

    private static Student findStudentById(String id){
        for (Student student : studentList){
            if (student.getId().equals(id)){
                return student;
            }
        }
        return null;
    }
}